import { _decorator, Component, instantiate, Node, UITransform, Vec3 } from 'cc';

import TileModel from '../define/TileModel';
import { UIConfig } from '../define/UIConfig';

const { ccclass, property } = _decorator;

@ccclass('TileBgCom')
export class TileBgCom extends Component {

    @property(Node)
    private readonly tileNode: Node = null;

    @property(Node)
    private readonly bgNode: Node = null;


    start() {
        this.initBg();
    }

    // 初始化背景格子
    private initBg() {
        if (!this.bgNode || !this.tileNode)
            return;
        // tile nodes
        for (let x = 0; x < UIConfig.COL; x++) {
            for (let y = 0; y < UIConfig.ROW; y++) {
                const node = this.addTile(x, y);
                node.setPosition(TileModel.getPos(x, y));
            }
        }
        // bg
        this.bgNode.getComponent(UITransform).setContentSize(TileModel.size);
    }
    private addTile(x: number, y: number): Node {
        let node: Node = null;
        if (x == 0 && y == 0)
            node = this.tileNode;
        else {
            node = instantiate(this.tileNode);
            this.bgNode.addChild(node);
        }
        node.scale = Vec3.ONE;
        node.getComponent(UITransform).setContentSize(UIConfig.tileSize());
        return node;
    }

}


